package com.yipei.business.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yipei.business.domain.MlAssetFlow;
import com.yipei.common.utils.SecurityUtils;
import com.yipei.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yipei.common.annotation.Log;
import com.yipei.common.core.controller.BaseController;
import com.yipei.common.core.domain.AjaxResult;
import com.yipei.common.enums.BusinessType;
import com.yipei.business.domain.MlAccountAssetApply;
import com.yipei.business.service.IMlAccountAssetApplyService;
import com.yipei.common.utils.poi.ExcelUtil;
import com.yipei.common.core.page.TableDataInfo;

/**
 * 出入资金Controller
 *
 * @author yipei
 * @date 2024-12-21
 */
@RestController
@RequestMapping("/business/apply")
@Api(tags = "出入资金", description = "出入资金管理")
public class MlAccountAssetApplyController extends BaseController
{
    @Autowired
    private IMlAccountAssetApplyService mlAccountAssetApplyService;

    /**
     * 查询出入资金列表
     */
    @ApiOperation("出入资金获取数据")
    //@PreAuthorize("@ss.hasPermi('business:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(MlAccountAssetApply mlAccountAssetApply)
    {
        startPage();
        List<MlAccountAssetApply> list = mlAccountAssetApplyService.selectMlAccountAssetApplyList(mlAccountAssetApply);
        return getDataTable(list);
    }

    /**
     * 导出出入资金列表
     */
    @ApiOperation("出入资金导出数据")
    //@PreAuthorize("@ss.hasPermi('business:apply:export')")
    @Log(title = "出入资金", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MlAccountAssetApply mlAccountAssetApply)
    {
        List<MlAccountAssetApply> list = mlAccountAssetApplyService.selectMlAccountAssetApplyList(mlAccountAssetApply);
        ExcelUtil<MlAccountAssetApply> util = new ExcelUtil<MlAccountAssetApply>(MlAccountAssetApply.class);
        util.exportExcel(response, list, "出入资金数据");
    }

    /**
     * 获取出入资金详细信息
     */
    @ApiOperation("出入资金通过ID获取数据")
    //@PreAuthorize("@ss.hasPermi('business:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mlAccountAssetApplyService.selectMlAccountAssetApplyById(id));
    }

    /**
     * 新增出入资金
     */
    //@PreAuthorize("@ss.hasPermi('business:apply:add')")
    @ApiOperation("出入资金新增数据")
    @Log(title = "出入资金", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MlAccountAssetApply mlAccountAssetApply)
    {
        return toAjax(mlAccountAssetApplyService.insertMlAccountAssetApply(mlAccountAssetApply));
    }

    /**
     * 修改出入资金
     */
    @ApiOperation("出入资金修改数据")
    //@PreAuthorize("@ss.hasPermi('business:apply:edit')")
    @Log(title = "出入资金", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MlAccountAssetApply mlAccountAssetApply)
    {
        return toAjax(mlAccountAssetApplyService.updateMlAccountAssetApply(mlAccountAssetApply));
    }

    /**
     * 删除出入资金
     */
    @ApiOperation("出入资金删除数据")
    //@PreAuthorize("@ss.hasPermi('business:apply:remove')")
    @Log(title = "出入资金", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mlAccountAssetApplyService.deleteMlAccountAssetApplyByIds(ids));
    }

    @ApiOperation("后台出入资金新增数据")
    @Log(title = "出入资金", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addData(@RequestBody MlAccountAssetApply mlAccountAssetApply) throws Exception {
        return toAjax(mlAccountAssetApplyService.add(mlAccountAssetApply));
    }

    @ApiOperation("出入资金作废")
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody MlAccountAssetApply mlAccountAssetApply)
    {
        return toAjax(mlAccountAssetApplyService.cancelMlAccountAssetApplyById(mlAccountAssetApply.getId()));
    }
    /**
     * 根据当前账户获取入
     */
    @ApiOperation("获取当前账户账户入金")
    @GetMapping("/transferintoList")
    public AjaxResult transferintoList(@RequestParam(name = "s_time", required = true) String s_time, @RequestParam(name = "e_time", required = true) String e_time)
    {
        startPage();
        Integer id = SecurityUtils.getLoginMember().getMlTradeAccount().getId();
        if(StringUtils.isNotEmpty(e_time)&&StringUtils.isNotEmpty(e_time)){
            e_time= e_time+" 24:59:59";
            List<MlAccountAssetApply> list = mlAccountAssetApplyService.lambdaQuery()
                    .eq(MlAccountAssetApply::getTdAccId,id)
                    .eq(MlAccountAssetApply::getCrLx, BigDecimal.ZERO)
                    .ge(MlAccountAssetApply::getCreateTime,s_time).le(MlAccountAssetApply::getCreateTime,e_time).orderByDesc(MlAccountAssetApply::getCreateTime)
                    .list();
            return AjaxResult.success(list);
        }else {
            List<MlAccountAssetApply> list = mlAccountAssetApplyService.lambdaQuery().eq(MlAccountAssetApply::getTdAccId,id).eq(MlAccountAssetApply::getCrLx, BigDecimal.ZERO).list();
            return AjaxResult.success(list);
        }
    }

    /**
     * 根据当前账户获取出金
     */
    @ApiOperation("获取当前账户账户出金")
    @GetMapping("/transferOut")
    public AjaxResult transferOut(@RequestParam(name = "s_time", required = true) String s_time, @RequestParam(name = "e_time", required = true) String e_time)
    {
        startPage();
        Integer id = SecurityUtils.getLoginMember().getMlTradeAccount().getId();
        if (StringUtils.isNotEmpty(e_time)&&StringUtils.isNotEmpty(e_time)){
            e_time= e_time+" 24:59:59";
            List<MlAccountAssetApply> list = mlAccountAssetApplyService.lambdaQuery()
                    .eq(MlAccountAssetApply::getTdAccId,id)
                    .eq(MlAccountAssetApply::getCrLx, BigDecimal.ONE)
                    .ge(MlAccountAssetApply::getCreateTime,s_time).le(MlAccountAssetApply::getCreateTime,e_time).orderByDesc(MlAccountAssetApply::getCreateTime)
                    .list();
            return AjaxResult.success(list);
        }else {
            List<MlAccountAssetApply> list = mlAccountAssetApplyService.lambdaQuery().eq(MlAccountAssetApply::getTdAccId,id).eq(MlAccountAssetApply::getCrLx, BigDecimal.ONE).list();
            return AjaxResult.success(list);
        }
    }
}
